

package ro.ase.catalog.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import ro.ase.catalog.beans.Authority;
import ro.ase.catalog.beans.Role;
import ro.ase.catalog.security.SecurityException;
import ro.ase.catalog.security.dao.IAuthorityDAO;
import ro.ase.catalog.security.dao.IRoleDAO;
import ro.ase.catalog.security.filter.FilterSecurityInterceptor;
import ro.ase.catalog.security.filter.MethodSecurityInterceptor;
import ro.ase.catalog.security.service.IAuthorityService;

import java.util.List;
import java.util.Set;


public class AuthorityServiceImpl implements IAuthorityService {

    @Autowired
    private IAuthorityDAO authorityDAO;
    @Autowired
    private IRoleDAO roleDAO;

    public void add(Authority authority) {
        authorityDAO.save(authority);
    }
    
    public Integer findByNameVagueCount(String name) {
        return authorityDAO.findByNameVagueCount(name);
    }

    public List<Authority> findByNameVaguePerPage(String name, int start, int limit) {
        return authorityDAO.findByNameVaguePerPage(name, start, limit);
    }  
    
    public List<Authority> findAuthsByRoleIdPerPage(int roleId, int start, int limit) {
        List<Authority> auths = authorityDAO.findByRoleIdPerPage(roleId,start,limit);
        return auths;
    }    

    public Integer findAuthsByRoleIdCount(int roleId) {
        return authorityDAO.findByRoleIdCount(roleId);
    }  
    
    public void bindAuths(int[] authIds, int roleId) {
        Role role = roleDAO.get(roleId);
        for (int i = 0; i < authIds.length; i++) {
            Authority auth = authorityDAO.get(authIds[i]);
            Set<Role> roles =  auth.getRoles();
            roles.add(role);
            auth.setRoles(roles);
        }

        FilterSecurityInterceptor.refresh();
        MethodSecurityInterceptor.refresh();
    }    
    
    public void unBindAuths(int[] authIds, int roleId) {
        Role role = roleDAO.get(roleId);
        for (int i = 0; i < authIds.length; i++) {
            Authority auth = authorityDAO.get(authIds[i]);
            Set<Role> roles =  auth.getRoles();
            roles.remove(role);
            auth.setRoles(roles);
        }
        
        FilterSecurityInterceptor.refresh();
        MethodSecurityInterceptor.refresh();
    } 
    
    public void delete(int[] authIds) throws SecurityException {
        for (int i = 0; i < authIds.length; i++) {
            Authority authority = authorityDAO.get(authIds[i]);
            
            if(authority.getRoles() != null && authority.getRoles().size() > 0) {
                throw new SecurityException("error.authority.delete.roleNotEmptyInAuthority");
            }   
            authorityDAO.delete(authority);
        }
    }
    
    public void update(Authority authority){
    	Authority auth = authorityDAO.get(authority.getId());
    	auth.setValue(authority.getValue());
    }
    
}
